#import "Common.h"
#import "InvaderBase.h"

void gCreateInvaders()
{
	CCSpriteFrameCache* cache = [CCSpriteFrameCache sharedSpriteFrameCache];
	[cache addSpriteFramesWithFile:[NSString stringWithFormat:@"invaders%@.plist", gHDSuffix]];
	
	for(int i=0; i<INVADERS_COUNT; i++)	{
		InvaderData& id = gInvadersData[i];
		
		id.frameArr = [[[NSMutableArray alloc] init] retain];

		if(id.frameCount > 0)
			for(int j=0; j<id.frameCount; j++)
				[id.frameArr addObject:[cache spriteFrameByName:[NSString stringWithFormat:@"%@-%d.png", id.fileName, j+1]]];
		else
			[id.frameArr addObject:[cache spriteFrameByName:[NSString stringWithFormat:@"%@.png", id.fileName]]];

		CCActionInterval* aniFrame;
		if(id.type == IT_Gold) 
			aniFrame = [CCAnimate actionWithAnimation:[CCAnimation animationWithFrames:id.frameArr delay:0.03]];
		else
			aniFrame = [CCAnimate actionWithAnimation:[CCAnimation animationWithFrames:id.frameArr delay:0.3]];
		
		id.animation = [CCRepeatForever actionWithAction:aniFrame];
		[id.animation retain];
	}
}

void gReleaseInvaders()
{
	for(int i=0; i<INVADERS_COUNT; i++)	{
		[gInvadersData[i].animation release];
		[gInvadersData[i].frameArr release];
	}
}

@implementation InvaderBase

@synthesize iType = mType, active = mActive, radius = mRadius;

-(id)initWithType:(InvaderType)type batch:(CCSpriteBatchNode*)batch
{
	if((self = [super initWithSpriteFrame:[gInvadersData[type].frameArr objectAtIndex:0]]))	{
		mType			= type;
		mActive			= false;
		mBatch			= batch;
		mRadius			= 20*gCurScore.sizeFactor;

		// 出现的方位，上45%，左25%，右25%，下5%
		int direction = rand() % 100;
		
		CGPoint startPos, endPos;
		
		if(direction < 45)			// 上
		{
			startPos.x = rand() % 490 - 30 + gSettingData.startPosX;
			startPos.y = 350;
			endPos.x = rand() % 230 + 100 + gSettingData.startPosX;
			endPos.y = -30;
		}
		else if(direction < 70)		// 左
		{
			startPos.x = -30;
			int top = rand() % 100;
			if(top < 75)		// 尽量出现在上部
				startPos.y = rand() % 190 + 160;
			else
				startPos.y = rand() % 190 - 30;
			endPos.x = 510;
			endPos.y = rand() % 120 + 100;
		}
		else if(direction < 95)		// 右
		{
			startPos.x = 510;
			int top = rand() % 100;
			if(top < 75)		// 尽量出现在上部
				startPos.y = rand() % 190 + 160;
			else
				startPos.y = rand() % 190 - 30;
			endPos.x = -30;
			endPos.y = rand() % 120 + 100;
		}
		else						// 下
		{
			startPos.x = rand() % 490 - 30 + gSettingData.startPosX;
			startPos.y = -30;
			endPos.x = rand() % 230 + 100 + gSettingData.startPosX;
			endPos.y = 350;
		}
		
		CGPoint nml = ccpNormalize(ccpSub(endPos, startPos));
		float rotate = 90 - CC_RADIANS_TO_DEGREES(ccpToAngle(nml));
		float speed = rand() % 80 + 50;
		
		mSpeed.x = nml.x * speed;
		mSpeed.y = nml.y * speed;
		
		if(gInvadersData[mType].animation != NULL) {
			CCAction* action = [gInvadersData[mType].animation copy];
			[self runAction:action];
			[action release];
		}
		
		self.position = startPos;
		self.rotation = rotate;
		
		[self autorelease];
	}
	
	return self;
}

-(void)fire
{
	mActive = true;
	[mBatch addChild:self];
}

-(void)destroy
{
	mActive = false;
	self.visible = false;
	[self runAction:[CCSequence actions:[CCDelayTime actionWithDuration:0.1],[CCCallFunc actionWithTarget:self selector:@selector(clear)], nil]];
}

-(void)clear
{
	[self stopAllActions];
	[self.parent removeChild:self cleanup:YES];
}

-(int)getScore
{
	if(gInvadersData[mType].needBonus)
		return gInvadersData[mType].baseScore*gCurScore.bonus*gCurScore.scoreFactor;
	else
		return gInvadersData[mType].baseScore*gCurScore.scoreFactor;
}

-(bool)updateInvader:(ccTime)dt
{
	if(mActive) {
		CGPoint pos = ccpAdd(self.position, ccpMult(mSpeed, dt*gCurScore.speedFactor));
		self.position = pos;
		self.scale = gCurScore.sizeFactor;

		mRadius	= 25*gCurScore.sizeFactor;
	
		if(pos.x<-50 || pos.x>530 || pos.y<-50 || pos.y>370) {
			[self destroy];
			return false;
		}
	}
	
	return true;
}

@end
